import requests
from bs4 import BeautifulSoup
import os

# 设置要爬取的网页地址和图片保存目录
url = "https://pc.yiyouliao.com/microsoft/article/rivers/newsfeed/1531576099383816194/ID00GRG8NWIVIQ3.html?channel=96b823c73c4d4ce5874d445a773acde3"
save_dir = "images"

# 发送请求并获取响应
response = requests.get(url)

# 如果请求成功
if response.status_code == 200:
    # 创建图片保存目录
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    # 解析网页内容
    soup = BeautifulSoup(response.text, "html.parser")

    # 获取网页文本内容并打印输出
    text = soup.get_text()

    print(text)


    # 获取所有图片标签
    img_tags = soup.find_all("img")

    # 依次下载所有图片
    for i, img_tag in enumerate(img_tags):
        img_url = img_tag.get("src")  # 获取图片地址
        if img_url.startswith("http"):
            filename = os.path.join(save_dir, f"image_{i}.jpg")  # 构造文件名
            try:
                # 下载图片并保存到文件
                with open(filename, "wb") as f:
                    f.write(requests.get(img_url).content)
                print(f"Downloaded image {i}: {img_url}")
            except Exception as e:
                print(f"Failed to download image {i}: {img_url}")
                print(e)
else:
    print(f"Failed to request {url}")

